package visualcbir.cluster;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Observable;
import visualcbir.IBackStructureView;
import visualcbir.Model;
import visualcbir.ModelComposed;
import visualcbir.ViewComposed;
import visualcbir.node.IViewNode;
import visualcbir.node.ModelNode;
import visualcbir.node.ViewNode;

/**
 *
 * @author sheila
 */

public class VSCircle extends ViewComposed{

    public VSCircle(ModelComposed oModel,  int piNumSubviews){        
        super(oModel, piNumSubviews);
    }

    @Override
     public IBackStructureView makeBackStructureView(){
        this.oIBackStructureView = new VSEdgesBackStructure();
        return this.oIBackStructureView;
    }

    /* Computes mathematical and trigonometric data needed to draw each object in the view.
     * Sets the points needed to draw images and backstructure. Backstructure is created
     */
    @Override
    public void update(Observable o, Object arg) {        
        //super.update(o,arg);
        double x,y,radio=175, thetaRadianes=0.0; //s/308  previa radio default era 320
        int iViewNumber=1;
        ArrayList<VSEdge> aoVSEdge=new ArrayList<VSEdge>();
        double angleIncrement = Math.round(360.0/(iNumSubviews-1));
        float widthline=43.0f;
        Color edgeColor = Color.red;        
        double scaleFactorSubViews = 1.03 * this.getScaleFactor();

        radio*=scaleFactorSubViews;

        if(this.getScaleFactor()<0.6)
            radio*=1.01; //radio*=0.92;

        super.getView(0).setCenterPoint(this.getCenterPoint().x, this.getCenterPoint().y);
        super.getView(0).setScaleFactor(scaleFactorSubViews);

        for(double theta=0 ; theta<360  &&  iViewNumber<this.getNumViews() && iViewNumber<this.iNumSubviews; theta+=angleIncrement){
            thetaRadianes = theta * (Math.PI/180);
            x = ( radio * Math.cos(thetaRadianes) + this.getCenterPoint().x );
            y = ( radio * Math.sin(thetaRadianes) + this.getCenterPoint().y );
            super.getView(iViewNumber).setCenterPoint((int)Math.round(x),(int)Math.round(y));
            super.getView(iViewNumber).setScaleFactor(scaleFactorSubViews-=0.0118);
            // edge format
            if(! (this.getView(iViewNumber) instanceof ViewComposed) ) widthline =1;
            //if(this.getView(iViewNumber) instanceof ViewComposed){ edgeColor = Color.white;}
            aoVSEdge.add(new VSEdge((IViewNode)this.getView(0), this.getView(iViewNumber).getCenterNode(), widthline, edgeColor));
            if(this.getView(iViewNumber) instanceof ViewComposed && widthline>7){ widthline-=7; edgeColor=edgeColor.darker();}            
            iViewNumber++;
        }
        
        super.oIBackStructureView.setObject(aoVSEdge);//3
        super.setViewDimension((int)radio*2 + 400, (int)radio*2 + 400);
        super.update(o,arg);
        //super.setCenterPoint(this.getViewDimension().width/2, this.getViewDimension().height/2);
        // it was a problem in Find Node. with composed structures because of this last line(set center point) that only serve for the general structure and not for the subviews
        //the subviews are drwaed adequatly but after that o center point of each one is setted in function of the relative size of the view and not in function of the overall structure.
        //solved calling the centerpoint of the centralnode instead of calling the structure center point.  
    }

    @Override
    public void addView(Model oModelAdded) {
         if(oModelAdded instanceof ModelNode)
            super.addView(new ViewNode((ModelNode)oModelAdded));
         else if(oModelAdded instanceof ModelComposed){
            super.addView(new VSCircle((ModelComposed)oModelAdded,17));
         } 
    }
}
